﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess;
using MShop.Core.Domain;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class OccasionController : SecureController
    {
        private ICommonDao<MShop.Core.Domain.Occasion> _OccasionDao;
        private ICommonDao<MShop.Core.Domain.OccasionProduct> _PDao;
        private readonly IProductDao _ProductDao;
        private readonly IItemCatDao _ItemCatDao;
        public OccasionController(ICommonDao<Occasion> occasionDao,ICommonDao<MShop.Core.Domain.OccasionProduct> pDao,
            IProductDao productDao,IItemCatDao itemCatDao)
        {
             _OccasionDao = occasionDao;
             _PDao= pDao;
             _ProductDao = productDao;
             _ItemCatDao = itemCatDao;
        }

        public ActionResult Index()
        {
            return View(_OccasionDao.GetAll());
        }


        public ActionResult DeleteOccasionProduct(int id,int oid)
        {
            var model = _PDao.Find(x => x.Id == id);
            _PDao.Delete(model);
            _PDao.SaveChanges();
            return RedirectToAction("ProductIndex", new { id = oid });
        }


        public ActionResult ProductIndex(int id)
        {
            return View(_PDao.Find(x=>x.OId==id));
        }

        public ActionResult SelectProduct(int oid,string key,int? cid,int? page)
        {
            var query = _ProductDao.Find(x => x.IsPublish && x.IsDelete == false && x.Status == 1);
            int pageIndex = 0;
            if (page.HasValue)
            {
                pageIndex = page.Value - 1;
            }
            if (!string.IsNullOrEmpty(key))
            {
                query = from item in query where item.ProductName.Contains(key) select item; 
            }
            if (cid.HasValue)
            {
                query = from item in query where item.CId == cid.Value select item;
            }
            query = query.OrderBy(x=>x.Sort);
            MShop.Web.Components.Paging.PagedList<Product> pagedlist = new Components.Paging.PagedList<Product>(query, pageIndex, 15);
            ViewData["itemcats"] = _ItemCatDao.BuildSelectList(null,true);
            ViewData["ossicationproduct"] = _PDao.Find(x => x.OId == oid);
            return View(pagedlist);
        }


        [HttpPost]
        public ActionResult SelectProduct(int oid, string key, int? cid, int? page,FormCollection form)
        {
            string pids = form["pid"];
            int pid=1;
            if (string.IsNullOrEmpty(pids))
            {
                return RedirectToAction("SelectProduct", new { oid = oid, key = key, cid = cid, page = page });
            }
            else
            {
                if (pids.Contains(','))
                {
                    foreach (string s in pids.Split(','))
                    {
                        pid=int.Parse(s);
                        OccasionProduct model = _PDao.FirstOrDefault(x => x.ProductId==pid && x.OId == oid);
                        if (model == null)
                        {
                             Product product = _ProductDao.GetById(pid);
                             model = new OccasionProduct()
                             {
                                 OId = oid,
                                 ProductId = pid,
                                 ProductName = product.ProductName,
                                 ProductDesc = product.Desc,
                                 StartDate = DateTime.Now,
                                 EndDate = DateTime.Now.AddYears(10),
                                 IsValid =true
                             };
                            _PDao.Save(model);
                            _PDao.SaveChanges();
                        }
                    }
                   
                }
                else
                {
                    pid = int.Parse(pids);
                    OccasionProduct model = _PDao.FirstOrDefault(x => x.ProductId == pid && x.OId == oid);
                    if (model == null)
                    {
                         model = new OccasionProduct() { OId = oid, ProductId = pid, StartDate = DateTime.Now, EndDate = DateTime.Now.AddYears(10) };
                        _PDao.Save(model);
                        _PDao.SaveChanges();
                    }
                }
                return RedirectToAction("SelectProduct", new { oid = oid, key = key, cid = cid, page = page });
            }
        }

        public ActionResult Create()
        {

            return View(new Occasion());
        }

        [HttpPost]
        public ActionResult Create(Occasion occasion)
        {

            if (ModelState.IsValid)
            {
                _OccasionDao.Save(occasion);
                _OccasionDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(occasion);
        }

        public ActionResult Edit(int id)
        {

            return View(_OccasionDao.GetById(id));
        }


        public ActionResult Delete(int id)
        {
            var occ = _OccasionDao.GetById(id);
            _OccasionDao.Delete(occ);
            _OccasionDao.SaveChanges();
            return RedirectToAction("index");
        }


        [HttpPost]
        public ActionResult Edit(int id, Occasion occ)
        {
            if (ModelState.IsValid)
            {
                var dbocc = _OccasionDao.GetById(id);
                UpdateModel<Occasion>(dbocc, new string[] { "OccasionName", "OccasionDesc" });
                _OccasionDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(occ);


        }
    }
}
